﻿using SelfManagement.Database.POCO;
using SelfManagement.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelfManagement.Services
{
    public class ArticleService : BaseService<Article>, IArticleService
    {
        /// <summary>
        /// 根据用户id获取用户的文章
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public IQueryable<Article> QueryUserArticles(string uid)
        {
            return Query(a => a.User_Id == uid).Where(a => !a.IsDelete);
        }

        /// <summary>
        /// 根据用户id和文章类型查找用户的文章
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public IQueryable<Article> QueryUserAriticlesByCategoryId(string uid, string categoryId)
        {
            return QueryUserArticles(uid).Where(a => a.Category_Id == categoryId);
        }

        /// <summary>
        /// 根据用户的id和文章id获取文章
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public Article GetUserArticleById(string uid, string articleId)
        {
            return QueryUserArticles(uid).Where(a => a.Id == articleId).FirstOrDefault();
        }

        /// <summary>
        /// 根据id获取文章
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public Article GetArticleById(string articleId)
        {
            return Query(a => a.Id == articleId).Where(a => !a.IsDelete).FirstOrDefault();
        }

        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public bool DeleteUserArticleById(string uid, string articleId)
        {
            return Delete(GetUserArticleById(uid, articleId));
        }

    }
}
